package com.efast.dmom.mes.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Supplier;
import com.efast.dmom.mes.datahandlers.entity.SupplierDataHandler;
import com.efast.dmom.mes.datahandlers.entity.SupplierPartDataHandler;
import com.efast.dmom.mes.service.SupplierService;
import com.efast.dmom.mes.util.StringVerifyUtil;

@Service
public class SupplierServiceImpl implements SupplierService {

	@Autowired
	private SupplierDataHandler supplierDataHandler;

	@Autowired
	private SupplierPartDataHandler supplierPartDataHandler;

	// add a supplier
//	@Override
	public ResultBean save(Supplier supplier) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (!StringUtil.isNotBlank(supplier.getInnerData().getName())
				|| !StringUtil.isNotBlank(supplier.getInnerData().getSupplierCode())
				|| !StringUtil.isNotBlank(supplier.getInnerData().getMobilePhone1())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查供应商编码、供应商名称、手机号1是否填写!");
			return resultBean;
		}
		// determine whether the supplier code is unique
		Supplier supplierEntityByCode = supplierDataHandler
				.getSupplierByCode(supplier.getInnerData().getSupplierCode());
		if (supplierEntityByCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("供应商编码不唯一!");
			return resultBean;
		}
		// determine whether the supplier name is unique
		Supplier supplierEntityByName = supplierDataHandler.getSupplierByCode(supplier.getInnerData().getName());
		if (supplierEntityByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("供应商名称不唯一!");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(supplier.getInnerData().getSupplierCode())) {
			  resultBean.setSuccess(false);
			  resultBean.setMessage("供应商编码不是由英文、字母、下划线组成!");
			  return resultBean;
		  }
		supplierDataHandler.save(supplier);
		resultBean.setSuccess(true);
		resultBean.setMessage("供应商信息保存成功!");
		return resultBean;
	}

	// delete a supplier
//	@Override
	public ResultBean delete(Supplier entity) {
		ResultBean resultBean = new ResultBean(true, ' ');
		// Determine if it is referenced by the supplier's part sheet
		int qty = supplierPartDataHandler.getSupplierPartCountBySupplierId(entity.getInnerData().getId());
		if (qty > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该条数据已被引用，无法进行删除!");
			return resultBean;
		}
		supplierDataHandler.delete(entity);
		resultBean.setSuccess(true);
		resultBean.setMessage("供应商信息删除成功!");
		return resultBean;
	}

	// update a supplier
//	@Override
	public ResultBean update(Supplier supplier) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (!StringUtil.isNotBlank(supplier.getInnerData().getName())
				|| !StringUtil.isNotBlank(supplier.getInnerData().getSupplierCode())
				|| !StringUtil.isNotBlank(supplier.getInnerData().getMobilePhone1())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查供应商编码、供应商名称、手机号1是否填写!");
			return resultBean;
		}
		Supplier result = supplierDataHandler.getById(supplier.getInnerData().getId());
		if (result.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的供应商不存在!");
			return resultBean;
		}

		if (!supplier.getInnerData().getSupplierCode().equals(result.getInnerData().getSupplierCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("供应商编号不可修改!");
			return resultBean;
		}
		
		if (!supplier.getInnerData().getName().equals(result.getInnerData().getName())) {
			Supplier supplierEntityByName = supplierDataHandler.getSupplierByCode(supplier.getInnerData().getName());
			if (supplierEntityByName.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改后的供应商名称不唯一!");
				return resultBean;
			}
		}
		supplierDataHandler.update(supplier);
		resultBean.setSuccess(true);
		resultBean.setMessage("供应商信息修改成功!");
		return resultBean;
	}

//	@Override
	public Supplier getById(long id) {
		return supplierDataHandler.getById(id);
	}

//	@Override
	public Supplier getByName(String name) {
		return supplierDataHandler.getByName(name);
	}

}
